﻿@using System.ComponentModel
@using HelloWorld.Code.Util
@model HelloWorld.Models.UsersList
@{
    ViewBag.Title = "Users list";
}
@using (Html.BeginForm("Index", "Users", FormMethod.Post, new {id = "frmSearch"}))
{            
    @Html.LabelFor(model => model.FirstName) 
    @Html.TextBoxFor(model => model.FirstName)
    <br/> 
    @Html.LabelFor(model => model.LastName) 
    @Html.TextBoxFor(model => model.LastName)   
    <input type="submit" name="Search" value="Search"/>   
}
@using (Html.BeginForm("Index", "Users", FormMethod.Post, new {id = "frmDelete"}))
{            
    <input type="hidden" id="hfDeleteId" name="hfDeleteId"/>
    @Html.HiddenFor(model => model.FirstName)
    @Html.HiddenFor(model => model.LastName)                                             
}
@if (!string.IsNullOrEmpty(ViewBag.DeleteMessage))
{
    <p style="color: @(ViewBag.Deleted ? "green" : "red")">@ViewBag.DeleteMessage</p>
}
@if (!Model.PagedList.CurrentPage.Any())
{
    <p>No users were found</p>
}
else
{
    <table>
        <thead>
            <tr>
                <th>@Html.ActionLink("First name", "Index", "Users",
                                    Url.GetRouteValueDictionaryForList(
                                        Model,
                                        sortColumn: "FirstName",
                                        direction: string.Compare(Model.SortColumn, "FirstName",
                                        StringComparison.CurrentCultureIgnoreCase) != 0 ? "asc" : Model.SortDirection == ListSortDirection.Ascending ? "desc" : "asc"), null)</th>
                <th>@Html.ActionLink("Last name", "Index", "Users",
                                    Url.GetRouteValueDictionaryForList(
                                        Model,
                                        sortColumn: "LastName",
                                        direction: string.Compare(Model.SortColumn, "LastName", StringComparison.CurrentCultureIgnoreCase) != 0 ? "asc" : Model.SortDirection == ListSortDirection.Ascending ? "desc" : "asc"), null)</th>
                <th>E-mail</th>
                <th>Role</th>
                <th>Actions</th>
            </tr>
        </thead>
        @foreach (var user in Model.PagedList.CurrentPage)
        {
            <tr>
                <td>@user.FirstName</td>
                <td>@user.LastName</td>
                <td>@user.Email</td>
                <td>@user.UserRole.UserRoleName</td>
                <td>
                    @Html.ActionLink("Edit", "Edit", "Users", new RouteValueDictionary { { "id", @user.UserId } }, null) | 
                    <a href="javascript:;" class="delete" data-id="@user.UserId">Delete</a>                   
                </td>
            </tr>
        } 
    </table>
@Html.PagedListPager(Url, new PagerHtmlRenderer(
                                                currentPageNumber: Model.PagedList.CurrentPageNumber,
                                                pageSize: Model.PagedList.PageSize,
                                                totalNumberOfItems: Model.PagedList.TotalNumberOfItems,
                                                actionName: "Index",
                                                controllerName: "Users",
                                                routeValues: Url.GetRouteValueDictionaryForList(Model),
                                                pageRouteValueName: "page"))
    
}
<br />
<br/>
@Html.ActionLink("Add a new user", "Add", "Users")

@section scripts
{
    <script type="text/javascript">
        $().ready(function () {
            $('.delete').click(function (event) {
                event.preventDefault();
                $("#hfDeleteId").val($(this).attr("data-id"));
                document.forms["frmDelete"].submit();
            });
        });
    </script>    
}
